package com.rds.material.service.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.rds.common.ServiceException;
import com.rds.common.api.material.domain.RdaPart;
import com.rds.common.api.material.domain.RdaPartDynamicattributevalue;
import com.rds.common.api.material.domain.RdaPartsRel;
import com.rds.common.utils.DateUtils;
import com.rds.common.utils.SecurityUtils;
import com.rds.material.dto.MaterialBillAndRdaPartsRel;
import com.rds.material.mapper.RdaPartMapper;
import com.rds.material.service.IRdaPartDynamicattributevalueService;
import com.rds.material.service.IRdaPartService;
import com.rds.material.service.IRdaPartsRelService;
import com.rds.material.utils.WorkspaceUtils;
import com.rds.system.service.IRdaPolicyService;
import com.rds.system.service.IRdaStateService;
import com.rds.system.service.IRdaTypeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

@Service
public class RdaPartServiceImpl implements IRdaPartService {
    @Autowired
    private IRdaPartsRelService rdaPartsRelService;

    @Autowired
    private RdaPartMapper rdaPartMapper;

    @Autowired
    private IRdaPartDynamicattributevalueService rdaPartDynamicattributevalueService;

    @Override
    public RdaPart selectRdaPartByid(Long id) {
        return rdaPartMapper.selectRdaPartByid(id);
    }

    @Override
    public List<RdaPart> selectRdaPartList(RdaPart rdaPart) {
        return rdaPartMapper.selectRdaPartList(rdaPart);
    }


    @Autowired
    private IRdaTypeService rdaTypeService;

    @Autowired
    private IRdaPolicyService rdaPolicyService;

    @Autowired
    private IRdaStateService rdaStateService;


    @Override
    public int insertRdaPart(RdaPart rdaPart) {
        rdaPart.setOwnerid(SecurityUtils.getUserId());
        rdaPart.setCreateTime(DateUtils.getNowDate());
        return rdaPartMapper.insertRdaPart(rdaPart);
    }

    @Override
    public int updateRdaPart(RdaPart rdaPart) {
        rdaPart.setModifyTime(DateUtils.getNowDate());
        return rdaPartMapper.updateRdaPart(rdaPart);
    }

    @Override
    public JSONObject getInfo(Long id) {
        if (id==0){
            throw new ServiceException("该物料未绑定任何ebom");
        }
        JSONObject jsonobj = new JSONObject();
        StringBuilder json=new StringBuilder();
        //查找关系表
        RdaPartsRel rdaPartsRel = rdaPartsRelService.selectBillByRelId(id);
        String stringJSON;
        MaterialBillAndRdaPartsRel materialBillAndRdaPartsRel = new MaterialBillAndRdaPartsRel(rdaPartsRel);
        stringJSON = JSON.toJSONString(materialBillAndRdaPartsRel);
        json.append(stringJSON);
        //查找库的动态属性
        RdaPartDynamicattributevalue rdaPartDynamicattributevalue=new RdaPartDynamicattributevalue();
        rdaPartDynamicattributevalue.setObjectid(rdaPartsRel.getId());
        List<RdaPartDynamicattributevalue> rdaPartDynamicattributevalueList=rdaPartDynamicattributevalueService.selectRdaPartDynamicattributevalueList(rdaPartDynamicattributevalue);
        System.out.println("rdaPartDynamicattributevalueList========================="+rdaPartDynamicattributevalueList);
        String attribute = "";
//        for(RdaPartDynamicattributevalue partDynamicattributevalue:rdaPartDynamicattributevalueList){
//            attribute += ",\""+partDynamicattributevalue.getAttributeName()+"\":\""+partDynamicattributevalue.getAttributeValue()==null?"":partDynamicattributevalue.getAttributeValue()+"\"";//字符串两边含有双引号
//        }
        for (RdaPartDynamicattributevalue partDynamicattributevalue : rdaPartDynamicattributevalueList) {
            String attributeValue = partDynamicattributevalue.getAttributeValue();
            attributeValue = attributeValue == null ? "" : attributeValue;
            attribute += ",\"" + partDynamicattributevalue.getAttributeName() + "\":\"" + attributeValue + "\"";
        }
        if(attribute!=null && attribute.length()>0){
            json.insert(json.length()-1,attribute);
            jsonobj.put("hasAttribute", attribute);
        }
        JSONObject jsonObject =  JSON.parseObject(json.toString());
        jsonobj.put("data",jsonObject);
        jsonobj.put("code",200);
        jsonobj.put("msg","操作成功");
        return jsonobj;
    }

    @Override
    public JSONObject getInfos(Long id) {
        RdaPartsRel rdaPartsRelf = new RdaPartsRel();
        rdaPartsRelf.setFromid(id);
        List<RdaPartsRel> rdapr = rdaPartsRelService.selectRdaPartsRelList(rdaPartsRelf);
        if (rdapr==null){
            throw new ServiceException("改物料为绑定任何关系");
        }
        RdaPartsRel rdaPartsRel1 = rdapr.get(0);
        id=rdaPartsRel1.getId();

        JSONObject jsonobj = new JSONObject();
        StringBuilder json=new StringBuilder();
        //查找关系表
        RdaPartsRel rdaPartsRel = rdaPartsRelService.selectBillByRelId(id);
        MaterialBillAndRdaPartsRel materialBillAndRdaPartsRel = new MaterialBillAndRdaPartsRel(rdaPartsRel);
        String stringJSON = JSON.toJSONString(materialBillAndRdaPartsRel);
        json.append(stringJSON);
        //查找库的动态属性
        RdaPartDynamicattributevalue rdaPartDynamicattributevalue=new RdaPartDynamicattributevalue();
        rdaPartDynamicattributevalue.setObjectid(rdaPartsRel.getId());
        List<RdaPartDynamicattributevalue> rdaPartDynamicattributevalueList=rdaPartDynamicattributevalueService.selectRdaPartDynamicattributevalueList(rdaPartDynamicattributevalue);
        System.out.println("rdaPartDynamicattributevalueList========================="+rdaPartDynamicattributevalueList);
        String attribute = "";
//        for(RdaPartDynamicattributevalue partDynamicattributevalue:rdaPartDynamicattributevalueList){
//            attribute += ",\""+partDynamicattributevalue.getAttributeName()+"\":\""+partDynamicattributevalue.getAttributeValue()==null?"":partDynamicattributevalue.getAttributeValue()+"\"";//字符串两边含有双引号
//        }
        for (RdaPartDynamicattributevalue partDynamicattributevalue : rdaPartDynamicattributevalueList) {
            String attributeValue = partDynamicattributevalue.getAttributeValue();
            attributeValue = attributeValue == null ? "" : attributeValue;
            attribute += ",\"" + partDynamicattributevalue.getAttributeName() + "\":\"" + attributeValue + "\"";
        }
        if(attribute!=null && attribute.length()>0){
            json.insert(json.length()-1,attribute);
            jsonobj.put("hasAttribute", attribute);
        }
        JSONObject jsonObject =  JSON.parseObject(json.toString());
        jsonobj.put("data",jsonObject);
        jsonobj.put("code",200);
        jsonobj.put("msg","操作成功");
        return jsonobj;
    }

    @Override
    public JSONObject getrelInfo(Long id) {
        JSONObject jsonobj = new JSONObject();
        StringBuilder json=new StringBuilder();
        //查找关系表
        RdaPartsRel rdaPartsRelF = new RdaPartsRel();
        rdaPartsRelF.setFromid(id);
        List<RdaPartsRel> rdaPartsRels = rdaPartsRelService.selectRdaPartsRelList(rdaPartsRelF);
        RdaPartsRel rdaPartsRel = rdaPartsRelService.selectBillByRelId(rdaPartsRels.get(0).getId());
        MaterialBillAndRdaPartsRel materialBillAndRdaPartsRel = new MaterialBillAndRdaPartsRel(rdaPartsRel);
        String stringJSON = JSON.toJSONString(materialBillAndRdaPartsRel);
        json.append(stringJSON);
        //查找库的动态属性
        RdaPartDynamicattributevalue rdaPartDynamicattributevalue=new RdaPartDynamicattributevalue();
        rdaPartDynamicattributevalue.setObjectid(rdaPartsRel.getId());
        List<RdaPartDynamicattributevalue> rdaPartDynamicattributevalueList=rdaPartDynamicattributevalueService.selectRdaPartDynamicattributevalueList(rdaPartDynamicattributevalue);
        System.out.println("rdaPartDynamicattributevalueList========================="+rdaPartDynamicattributevalueList);
        String attribute = "";
        for(RdaPartDynamicattributevalue partDynamicattributevalue:rdaPartDynamicattributevalueList){
            attribute += ",\""+partDynamicattributevalue.getAttributeName()+"\":\""+partDynamicattributevalue.getAttributeValue()+"\"";//字符串两边含有双引号
        }
        if(attribute!=null && attribute.length()>0){
            json.insert(json.length()-1,attribute);
            jsonobj.put("hasAttribute", attribute);
        }
        JSONObject jsonObject =  JSON.parseObject(json.toString());
        jsonobj.put("data",jsonObject);
        jsonobj.put("code",200);
        jsonobj.put("msg","操作成功");
        return jsonobj;
    }

    @Override
    public int deleteRdaPartByIds(Long[] ids) {
        return rdaPartMapper.deleteRdaPartByIds(ids);
    }

    @Override
    public int deleteMaterialBillTrees(Long[] ids) {
        return rdaPartMapper.deleteMaterialBillTrees(ids);
    }

    @Override
    public int deleteRdaPartById(Long id) {
        return rdaPartMapper.deleteRdaPartById(id);
    }

    @Override
    public List<RdaPart> selectRdaPartTreeList(RdaPart rdaPart) {
        return rdaPartMapper.selectRdaPartTreeList(rdaPart);
    }

    @Override
    public List<RdaPart> selectRdaPartTreeSubordinate(Long projectid,Long typeid) {
        return rdaPartMapper.selectRdaPartTreeSubordinate(projectid,typeid);
    }

    @Override
    public int deleteRdaPartByLevel(Long[] ids) {
        return rdaPartMapper.deleteRdaPartByLevel(ids);
    }

    @Override
    public List<RdaPart> partList(RdaPart rdaPart) {
        Map<String,Object> dmap= WorkspaceUtils.getTypePolicyStates("MaterialSeries","1",rdaTypeService,rdaPolicyService,rdaStateService);
        RdaPart rp = new RdaPart();
        if(dmap!=null){
            Map<String,Long> dtypeid=(Map<String,Long>)dmap.get("Typeid");
            Long typeid=dtypeid.get("SparePartsType");
            rp.setTypeid(typeid);
        }
        /**
         * 活动rdapart中的零件
         */
        List<RdaPart> rdaParts = rdaPartMapper.selectRdaPartList(rp);

        return rdaPartMapper.selectRdaPartList(rp);
    }
}
